package com.hsjk.szwj.module.train.controller.admin;

import com.hsjk.szwj.framework.common.pojo.CommonResult;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminCreateOrUpdateSkuBindReqVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminProductSkuBindListRespVO;
import com.hsjk.szwj.module.train.convert.ProductConvert;
import com.hsjk.szwj.module.train.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.hsjk.szwj.framework.common.pojo.CommonResult.success;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/09/12 00:27
 **/
@Tag(name = "后台管理 - 商品Sku绑定管理")
@RestController
@RequestMapping("/train/product-sku-bind")
@Validated
@Slf4j
public class AdminProductSkuBindController {
    @Resource
    private IProductService productService;


    @GetMapping("/list-by-sku-id")
    @Operation(summary = "/获取 sku 绑定的列表")
    @PreAuthorize("@ss.hasPermission('train:product-sku-bind:query')")
    public CommonResult<List<AdminProductSkuBindListRespVO>> getProductSkuBindListBySkuId(@RequestParam("skuId") Long skuId) {
        return success(ProductConvert.INSTANCE.convertAdminProductSkuBindListRespVO(
                productService.getProductSkuBindListBySkuId(skuId)));
    }

    @PostMapping("/create-or-update")
    @Operation(summary = "/绑定课程或班型")
    @PreAuthorize("@ss.hasPermission('train:product-sku-bind:update')")
    public CommonResult<Boolean> createOrUpdateProductSkuBind(@RequestBody @Valid
                                                              AdminCreateOrUpdateSkuBindReqVO reqVO) {
        return success(productService.createOrUpdateProductSkuBind(reqVO));
    }

    @PostMapping("/delete")
    @Operation(summary = "/删除绑定")
    @PreAuthorize("@ss.hasPermission('train:product-sku-bind:delete')")
    public CommonResult<Boolean> deleteProductSkuBind(@RequestParam("id") Long id) {
        return success(productService.deleteProductSkuBind(id));
    }
}
